package Server;

import java.io.File;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class ZitateController implements Observer {

	@FXML
	private ResourceBundle resources;

	@FXML
	private URL location;

	@FXML
	private Label bluePointsLabel;

	@FXML
	private Label redPointsLabel;

	@FXML
	private Label teamBuzzerLabel;

	private NetworkHandler network;
	private boolean buzzed = false;

	@FXML
	void initialize() {

	}

	public void setPoints(String bluePoints, String redPoints) {
		bluePointsLabel.setText(bluePoints);
		redPointsLabel.setText(redPoints);
	}

	public void setNetwork(NetworkHandler network) {
		this.network = network;
		network.addObserver(this);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		final String message = (String) arg1;
		System.out.println(message+"zitate");
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				if (!buzzed) {
					if (message.contains("red") || message.contains("blue")) {
						try {
						new MediaPlayer(new Media(getClass().getResource("BUZZER.WAV").toURI().toString())).play();
						} catch(Exception e){}
						buzzed = true;
						if(message.contains("blue")){
							teamBuzzerLabel.setText("Blau");
						}else{
							teamBuzzerLabel.setText("Rot");
						}
					}
				}
			}
		});
	}

	public void resetBuzzed() {
		buzzed = false;
		teamBuzzerLabel.setText("...");
	}

}
